package gestionSimpleATester;

import static org.junit.Assert.*;

import java.util.Vector;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class AccountTest {

	Account a0;
	
	Account a1;
	
	@Before
	public void setUp() throws Exception {
		a0 = new Account(new Person("Nom"),0f,0f,0);
	}

	@After
	public void tearDown() throws Exception {
		a0 = null;
	}

	@Test
	public void testDeposit() {
		float f = a0.getBalance();
		
		/**
		 * BUG FOUND with the test below
		 */
		try{
		a0.deposit(-1);
		}
		catch(Exception e){
			assertEquals("assertEquals deposit(-1)", f, a0.getBalance());
		}
		
		try{
		a0.deposit(0);
		assertEquals("assertEquals deposit(0)", f, a0.getBalance());
		}
		catch(Exception e){
			fail("Account.deposit(0) thrown an unexpected exception");
		}
		
		try{
		a0.deposit(100);
		assertEquals("assertEquals deposit(100)", f+100, a0.getBalance());
		}
		catch(Exception e){
			fail("Account.deposit(100) thrown an unexpected exception");
		}
	}
	
	
	
	@Test
	public void testWithdraw() throws Exception {
		a0.setBalance(0);
		a0.setOverdraft(0);
		
		/**
		 * BUG FOUND with the test below
		 */
		try{
			a0.withdraw(-1);
			fail("the withdrawal of -1 should throw an exception");
		}
		catch( Exception e){
			assertEquals("withdrawal(-1) shouldn't affect the balance", 0, a0.getBalance());
		}
		
		
		a0.setBalance(0);
		a0.withdraw(0);
		assertEquals("withdrawal(0) shouldn't affect the balance", 0, a0.getBalance());
		
		
		try{
			a0.setBalance(0);
			a0.withdraw(100);
			fail("withdrawal(100) should throw an exeption because the overdraft is 0 and the balance is 0" );
		}
		catch(Exception e){
			assertEquals( 0, a0.getBalance());
		}
		
		a0.setBalance(100);
		a0.setOverdraft(50);
		try{
			a0.withdraw(150);
		}
		catch(Exception e){};
		assertEquals(-50, a0.getBalance());

		/**
		 * BUG FOUND with the test below
		 */
		
		try{
		a0.setBalance(100);
		a0.withdraw(151);
		fail();
		}
		catch(Exception e){
			assertEquals(100, a0.getBalance());
		}
		
		
		a0.setBalance(100);
		a0.withdraw(80);
		assertEquals(20, a0.getBalance());
	}

	@Test
	public void testGetHistory() {
		fail("Not yet implemented");
	}

	@Test
	public void testSetHistory() {
		fail("Not yet implemented");
	}

	@Test
	public void testHistoryIterator() {
		fail("Not yet implemented");
	}

	@Test
	public void testAddHistory() {
		fail("Not yet implemented");
	}

	@Test
	public void testRemoveHistory() {
		fail("Not yet implemented");
	}

	@Test
	public void testIsHistoryEmpty() {
		fail("Not yet implemented");
	}

	@Test
	public void testClearHistory() {
		fail("Not yet implemented");
	}

	@Test
	public void testContainsHistory() {
		fail("Not yet implemented");
	}

	@Test
	public void testContainsAllHistory() {
		fail("Not yet implemented");
	}

	@SuppressWarnings("unchecked")
	@Test
	public void testHistorySize() {
		a0.setHistory(new Vector());
		assertEquals(0,a0.historySize());
		Vector v = new Vector();
		v.add("1");
		v.add(2);
		v.add(3.0);
		a0.setHistory(v);
		assertEquals(3,a0.historySize());
	}

	@Test
	public void testHistoryToArray() {
		fail("Not yet implemented");
	}

	@Test
	public void testGetOverdraft() {
		a0.setOverdraft(0);
		assertEquals(0, a0.getOverdraft());
		a0.setOverdraft(-10);
		assertEquals(-10, a0.getOverdraft());
		a0.setOverdraft(1);
		assertEquals(1, a0.getOverdraft());
	}

	@Test
	public void testSetOverdraft() {
		a0.setOverdraft(0);
		assertEquals(0, a0.getOverdraft());
		a0.setOverdraft(-10);
		assertEquals(-10, a0.getOverdraft());
		a0.setOverdraft(1);
		assertEquals(1, a0.getOverdraft());
	}

	@Test
	public void testGetBalance() throws Exception {	
		a0.setBalance(-10);
		assertEquals(-10, a0.getBalance());
		a0.setBalance(0);
		assertEquals(0, a0.getBalance());
		a0.setBalance(1);
		assertEquals(1, a0.getBalance());
	}

	@Test
	public void testSetBalance() {
		a0.setBalance(-10);
		assertEquals(-10, a0.getBalance());
		a0.setBalance(0);
		assertEquals(0, a0.getBalance());
		a0.setBalance(1);
		assertEquals(1, a0.getBalance());
	}

	@Test
	public void testGetNumber() {
		a0.setNumber(0);
		assertEquals(0, a0.getNumber());
		a0.setNumber(-1);
		assertEquals(-1, a0.getNumber());
		a0.setNumber(1);
		assertEquals(1, a0.getNumber());
	}

	@Test
	public void testSetNumber() {
		a0.setNumber(0);
		assertSame(0, a0.getNumber());
		a0.setNumber(-1);
		assertSame(-1, a0.getNumber());
		a0.setNumber(1);
		assertSame(1, a0.getNumber());
	}

	@Test
	public void testGetOwner() {
		Person p = new Person("p");
		a0.setOwner(p);
		assertSame(p, a0.getOwner());
	}

	@Test
	public void testSetOwner() {
		Person p = new Person("p");
		a0.setOwner(p);
		assertSame(p, a0.getOwner());
	}

}
